<template>
	<view>

		<view class="contain">
			<view class="top_title">
				等待支付
			</view>
			<navigator url="../mine/address/address?type=select" class="choice_address" v-if="!address.name">
				去选择收货地址
			</navigator>
			<navigator url="../../address/address?type=select" class="address_c" v-else>
				<text>姓名：{{user.name}} 电话号码：{{user.phone}}</text>
				<text>收货地址：{{user.address}}</text>
			</navigator>
		</view>
		<OrderList :orderListProp="orderList" :orderBaseProp="orderBase">
			<template v-slot:btn_bar>
				<view class="sure_pay">
					<view class="btn" @tap="sub">
						确认付款
					</view>
				</view>
			</template>
		</OrderList>
	</view>
</template>

<script>
	import request from '../../api/request.js';
	import OrderList from '../../components/orderList.vue'
	export default {
		data() {
			return {
				orderBase: {},
				orderList: [],
				paramsOrderList: [],
				orderNumber: [],
				totalNum: 0,
				address: {},
				user: {
					name: '',
					phone: '',
					address: ''
				}
			};
		},
		methods: {
			// 请求订单数据
			async initRequest(id) {
				const res = await request({
					url: `/order/getPreOrderById/${id}`
				})
				console.log(res);
				this.orderList = res.data.data.orderItems;
				this.orderBase = res.data.data.orderBase;
				this.orderList.forEach(item => {
					const obj = {};
					obj.cartId = this.orderList.id;
					obj.productId = this.orderList.productId;
					obj.productQuantity = this.orderList.productQuantity;
					obj.productSkuId = this.orderList.productSkuId;
					this.paramsOrderList.push(obj)
				})
				this.totalNum = res.data.data.orderBase.totalAmount;
			},
			async sub() {
				const params = {
					addressId: this.address.id,
					orderId: this.orderNumber,
					orderItemList: this.paramsOrderList,
					payType: 0,
					sourceType: 1
				};
				const res = await request({
					url: '/order/addConfirmOrder',
					data: params,
					method: "post"
				});
				console.log(res);
			}
		},
		onShow() {
			this.address = uni.getStorageSync('selectAddress') || {};
			this.user.name = this.address.name;
			this.user.phone = this.address.phoneNumber;
			this.user.address = `${this.address.city}-${this.address.province || ''}-${this.address.region || ''}`

		},
		onLoad(option) {
			this.orderNumber = option.id;
			this.initRequest(option.id);
		},
		components: {
			OrderList
		}
	}
</script>

<style lang="scss" scoped>
	@import './pay.scss';
</style>
